package com.hatkisoft.LockFunction.listener;

import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Keypad;
import blackberry.util.hotkey.helper.ProgramaticFilter;

public class GlobalKeyListenerOfLockApp implements KeyListener{

	private ProgramaticFilter filter = new ProgramaticFilter();
	
	public boolean keyDown(int keycode, int time) {
		
		if (filter.isProgramatic(time))
			return false;

		if (Keypad.key(keycode) == Keypad.KEY_LOCK)
			pushScreen();
			
		if (isCloseKeys(keycode)){
			popScreen();
		}
		return false;
	}

	private void popScreen() {
		ApplicationManager.getApplicationManager().postGlobalEvent(GlobalEventListenerOfLockApp.POP_SCREEN);
	}
	
	private void pushScreen(){
		ApplicationManager.getApplicationManager().postGlobalEvent(GlobalEventListenerOfLockApp.PUSH_SCREEN);
	}

	private boolean isCloseKeys(int keycode) {
		return Keypad.key(keycode) == Keypad.KEY_END
				|| Keypad.key(keycode) == Characters.ESCAPE;
//				|| Keypad.key(keycode) == Keypad.KEY_MENU
//				|| Keypad.key(keycode) == Keypad.KEY_SEND;
	}
	
	public boolean keyChar(char key, int status, int time) { return false; } 
	public boolean keyRepeat(int keycode, int time) { return false; } 
	public boolean keyStatus(int keycode, int time) { return false; }
	public boolean keyUp(int keycode, int time) { return false; }
}